package com.zw.a.basic.a11.demo01_set;

import com.itheima.pojo.Student;

import java.util.HashSet;

//案例: 演示HashSet存储在自定义对象, 并保证唯一性.
//细节: 记得在自定义类中重写hashCode()和equals()方法.
public class Demo04 {
    public static void main(String[] args) {
        //1.定义学生类, 属性为姓名和年龄.
        //2.创建HashSet集合, 用来存储学生对象, 并往其中添加3个学生的信息.
        HashSet<Student> hs = new HashSet<>();
        hs.add(new Student("刘亦菲", 33));
        hs.add(new Student("赵丽颖", 31));
        hs.add(new Student("赵丽颖", 31));
        hs.add(new Student("高圆圆", 35));
        hs.add(new Student("高圆圆", 35));

        //3.遍历集合, 并把结果打印到控制台上.
        for (Student student : hs) {
            System.out.println(student);
        }
    }
}
